/*
 * Copyright Miroslav Pokorny
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package lovely.gwt.jsonrpc.rebind.post;

import rocket.generator.rebind.codeblock.CodeBlock;
import rocket.generator.rebind.codeblock.StringLiteral;
import rocket.generator.rebind.codeblock.TemplatedFileCodeBlock;
import rocket.generator.rebind.method.Method;
import rocket.generator.rebind.type.Type;
import rocket.util.client.Checker;

/**
 * An abstraction for the json-rpc-invoker template file.
 * 
 * @author Miroslav Pokorny
 */
public class JsonRpcInvokerTemplatedFile extends TemplatedFileCodeBlock {

	public JsonRpcInvokerTemplatedFile() {
		super();
	}

	/**
	 * The serializer type for the model
	 */
	private Type serializerType;

	protected Type getSerializerType() {
		Checker.notNull("field:serializerType", serializerType);
		return this.serializerType;
	}

	public void setSerializerType(final Type serializerType) {
		Checker.notNull("parameter:serializerType", serializerType);
		this.serializerType = serializerType;
	}

	/**
	 * The key of the return template file
	 */
	private String returnTemplateKey;

	protected String getReturnTemplateKey() {
		Checker.notNull("field:returnTemplateKey", returnTemplateKey);
		return this.returnTemplateKey;
	}

	public void setReturnTemplateKey(final String returnTemplateKey) {
		Checker.notNull("parameter:returnTemplateKey", returnTemplateKey);
		this.returnTemplateKey = returnTemplateKey;
	}

	/**
	 * The key of the parameter template file
	 */
	private String parameterTemplateKey;

	protected String getParameterTemplateKey() {
		Checker.notNull("field:parameterTemplateKey", parameterTemplateKey);
		return this.parameterTemplateKey;
	}

	public void setParameterTemplateKey(final String parameterTemplateKey) {
		Checker.notNull("parameter:parameterTemplateKey", parameterTemplateKey);
		this.parameterTemplateKey = parameterTemplateKey;
	}

	/**
	 * The service method being executed
	 */
	private Method method;

	protected Method getMethod() {
		Checker.notNull("field:method", method);
		return this.method;
	}

	public void setMethod(final Method method) {
		Checker.notNull("parameter:method", method);
		this.method = method;
	}

	/**
	 * The request parameterType being executed
	 */
	private String requestParameterType;

	protected String getRequestParameterType() {
		Checker.notNull("field:requestParameterType", requestParameterType);
		return this.requestParameterType;
	}

	public void setRequestParameterType(final String requestParameterType) {
		Checker.notNull("parameter:requestParameterType", requestParameterType);
		this.requestParameterType = requestParameterType;
	}

	/**
	 * THe parameter type of the only parameter
	 */
	private Type parameterType;

	protected Type getParameterType() {
		Checker.notNull("field:parameterType", parameterType);
		return this.parameterType;
	}

	public void setParameterType(final Type parameterType) {
		Checker.notNull("parameter:parameterType", parameterType);
		this.parameterType = parameterType;
	}

	/**
	 * THe return type of the service.
	 */
	private Type returnType;

	protected Type getReturnType() {
		Checker.notNull("field:returnType", returnType);
		return this.returnType;
	}

	public void setReturnType(final Type returnType) {
		Checker.notNull("parameter:returnType", returnType);
		this.returnType = returnType;
	}

	@Override
	protected String getResourceName() {
		return JsonConstants.INVOKER_TEMPLATE;
	}

	protected CodeBlock getParameterTemplate() {
		final WriteObjectTemplatedFile template = new WriteObjectTemplatedFile();
		template.setParameterType(this.getParameterType());
		template.setTemplate(this.getParameterTemplateKey());
		return template;
	}

	protected CodeBlock getReturnTemplate() {
		final ReadObjectTemplatedFile template = new ReadObjectTemplatedFile();
		template.setReturnType(this.getReturnType());
		template.setSerializerType(this.getSerializerType());
		template.setTemplate(this.getReturnTemplateKey());
		return template;
	}

	@Override
	protected Object getValue0(final String name) {
		Object value = null;
		while (true) {
			if (JsonConstants.INVOKER_PARAMETER_TYPE.equals(name)) {
				value = this.getParameterType();
				break;
			}
			if (JsonConstants.INVOKER_RETURN_TYPE.equals(name)) {
				value = this.getReturnType();
				break;
			}
			if (JsonConstants.INVOKER_SERIALIZER_TYPE.equals(name)) {
				value = this.getSerializerType();
				break;
			}
			if (JsonConstants.INVOKER_METHOD_NAME.equals(name)) {
				value = new StringLiteral(this.getMethod().getName());
				break;
			}
			if (JsonConstants.REQUEST_PARAMETERS_RETURN_TEMPLATE.equals(name)) {
				value = this.getReturnTemplate();
				break;
			}
			if (JsonConstants.REQUEST_PARAMETERS_TEMPLATE.equals(name)) {
				value = this.getParameterTemplate();
				break;
			}
			if (JsonConstants.INVOKER_REQUEST_PARAMETER_TYPE.equals(name)) {
				value = new StringLiteral(this.getRequestParameterType());
				break;
			}
			break;
		}
		return value;
	}
}
